<template>
  <div class="site-wrapper">
    <div class="header clearfix">
      <el-button size="small" type="primary" @click="createTask">新增</el-button>
      <el-button size="small" type="primary" :disabled="multipleSelection.length === 0" @click="del(false)">删除</el-button>
      <div class="filter" @click="showFilter = true;"><img src="../../../../assets/livolobim/filter.png" alt="">搜索</div>
      <div class="mask" v-if="showFilter" @click="showFilter = false;"></div>
      <div class="filter-wrapper" v-if="showFilter">
        <span class="filter-arrow"></span>
        <span class="filter-white"></span>
        <div class="filter-header">搜索</div>
        <el-form style="padding: 0 16px;" label-width="120px">
          <el-form-item label="收件人信息">
            <el-input placeholder="收件人邮箱或昵称" v-model="filterData.receiveAccount"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button style="float:right;margin-right: 16px;" size="small" type="primary" @click="searchItem(true)">确定</el-button>
          <el-button style="float:right;margin-right: 16px;" size="small" @click="resetFilter">重置</el-button>
        </div>
      </div>
    </div>
    <div class="data-wrapper">
      <el-table :height="tableHeight" :data="tableData" border style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column align="center" type="selection" width="55"></el-table-column>
        <el-table-column align="center" prop="nickname" label="昵称" min-width="100"></el-table-column>
        <el-table-column align="center" prop="receiveAccount" label="邮箱地址" min-width="100"></el-table-column>
        <el-table-column label="操作" min-width="180" align="center">
          <template slot-scope="scope">
            <el-button @click="openEdit(scope.row, true)" type="text" size="small">查看</el-button>
            <el-button @click="openEdit(scope.row, false)" type="text" size="small">编辑</el-button>
            <el-button @click="del(scope.row.id)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style=""
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="page"
        :page-sizes="[15, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
      <el-dialog width="1000px" :visible.sync="showForm" :show-close="true">
        <div slot="title">
          <h2 style="text-align:center;margin-bottom:0;">{{isCheck? '查看': emailForm.id? '编辑': '新增'}}联系人</h2>
        </div>
        <div class="form-wrapper" style="padding: 0 20px;">
          <el-form ref="form" label-width="120px">
             <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item style="font-weight: bold;" label="用户名：">
                  <span slot="label">收件人地址<span style="color: red;">*</span>：</span>
                  <el-input :disabled="isCheck" v-model="emailForm.receiveAccount"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item style="font-weight: bold;" label="收件人昵称：">
                  <span slot="label">收件人昵称<span style="color: red;">*</span>：</span>
                  <el-input :disabled="isCheck" v-model="emailForm.nickname"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item>
              <el-button style="float:right;" size="small" @click="showForm = false">关闭</el-button>
              <el-button v-if="!isCheck" style="float:right;margin-right: 10px;" size="small" type="primary" @click="saveForm">创建</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import dingMixin from "@/mixins/ding";
import formUtil from "@/utils/formUtil";
import {dateUtil} from '@/utils/dateUtil'

export default {
  data() {
    return {
      showPeopleBtn: false,
      showFilter: false,
      loading:false,
      showForm: false,
      tableHeight: window.tableHeight,
      tableData: [],
      page:1,
      pageSize: 15,
      totalPage: 1,
      formVerify: {
        receiveAccount: '请输入收件人地址',
        nickname: '请输入收件人昵称'
      },
      filterData: {
        receiveAccount: '',
      },
      emailForm: {
        receiveAccount: '',
        nickname: ''
      },
      isCheck: false,
      multipleSelection: []
    }
  },
  mixins: [dingMixin],
  async mounted() {
    this.$nextTick(() => {
      this.tableHeight = document.documentElement['clientHeight'] - 170;
    })
    window.onresize = () => {
      this.tableHeight = document.documentElement['clientHeight'] - 170
    }
    this.searchItem(true);
  },
  methods: {
    resetFilter() {
      this.filterData = {
        receiveAccount: ''
      }
    },
    del(id) {
      let ids = id ? [id]: this.multipleSelection.map(item => item.id);
      formUtil.submitConfirm('您确定要删除联系人吗？', async ()=> {
        if(!await this.$httpN.postB('crm/mail/addressee/delete', {ids: ids}, false)) return;
        formUtil.submitSuccess('删除成功');
        this.searchItem(true);
      })
    },
    createTask() {
      this.showForm = true;
      this.isCheck = false;
      formUtil.resetData(this.emailForm);
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    async openEdit(target, isCheck) {
      let newForm = formUtil.deepCopy(target);
      this.emailForm = newForm;
      this.showForm = true;
      this.isCheck = isCheck;
    },
    async saveForm() {
      if(!formUtil.verifyData(this.emailForm, this.formVerify)) return;
      if(!await this.$httpN.postB('crm/mail/addressee/save', this.emailForm)) return;
      formUtil.submitSuccess('成功');
      this.showForm = false;
      this.searchItem(true);
    },
    sizeChangeHandle(limit) {
      this.pageSize = limit;
      this.searchItem(true);
    },
    currentChangeHandle(page) {
      this.page = page;
      this.searchItem();
    },
    async searchItem(init) {
      if(init) this.page = 1;
      let data = await this.$httpN.postB('crm/mail/addressee/list',{
        receiveAccount: this.filterData.receiveAccount,
        page: this.page,
        limit: this.pageSize,
      })
      this.tableData = data.list;
      this.totalPage = data.totalCount
    }
  }
}
</script>

<style lang="scss" scoped>
  .header{
    position: relative;
    padding: 10px 0 0 20px ;
    .filter{
      cursor: pointer;
      float: right;
      margin-right: 30px;
      line-height: 30px;
      img{
        width: 16px;
        margin-right: 3px;
      }
    }
    .mask{
      position: fixed;
      height: 100vh;
      width: 100%;
      top: 0;
      right: 0;
      z-index: 3;
    }
    .filter-wrapper{
      position: absolute;
      width: 455px;
      padding-bottom: 20px;
      background: white;
      top: 48px;
      right: 20px;
      z-index: 99;
      border-radius: 5px;
      box-shadow: 0 0 3px black;
      .filter-header{
        line-height: 40px;
        padding-left: 16px;
        border-bottom: 1px solid #DDD;
        margin-bottom: 20px;
      }
      .filter-arrow{
        width:12px;
        height:12px;
        position: absolute;
        top: -7px;
        right: 20px;
        box-shadow: 0 0 3px black;
        transform: rotate(45deg);
      }
      .filter-white{
        width:18px;
        height:18px;
        position: absolute;
        top: -2px;
        right: 17px;
        border-radius: 3px;
        background: white;
      }
    }
  }
  .data-wrapper{
    padding: 0 20px;
    margin-top: 10px;
  }
  .select-btn{
    display: inline-block;
    font-weight: normal;
    margin-right: 5px;
    width: 80px;
    height: 28px;
    border-radius: 30px;
    background: #FAFAFA;
    border: 1px solid #ECECEC;
    position: relative;
    line-height: 28px;
    color: #7982A7;
    font-size: 12px;
    text-align: center;
    position: relative;
    cursor: pointer;
    .del{
      position: absolute;
      top:0;
      left: 0;
      width: 100%;
      height: 100%;
      border-radius: 30px;
      color: white;
      text-align: center;
      background:rgba(130, 130, 130, 0.9);
      display: none;
    }
    &:hover{
      .del{
        display: block;
      }
    }
  }
  .fileitem{
    line-height: 32px;
    margin-bottom: 5px;
    background: #F5F5F5;
    width: 474px;
    padding-left:15px;
    font-size: 12px;
    .uploadtip{
      float:right;
      margin-right: 20px;
      cursor: pointer;
    }
  }
  .addfilebtn{
    cursor: pointer;
    color: #909399;
    display: block;
    font-weight: normal;
  }
</style>